package com.lvtongka.app.base

import android.annotation.SuppressLint
import android.content.Intent
import android.os.Bundle
import android.provider.MediaStore
import androidx.core.content.FileProvider
import androidx.viewbinding.ViewBinding
import java.io.File
import java.text.SimpleDateFormat
import java.util.Date

abstract class BaseActivity<T : ViewBinding, VM : BaseViewModel> : BaseVBActivity<T>() {
    lateinit var viewModel: VM
    // 拍照图片路径 暂未使用
    protected var filePath: File? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewModel = initViewModel()
        initEvent()
        initData()
    }

    protected abstract fun initViewModel(): VM

    @SuppressLint("SimpleDateFormat")
    protected fun dispatchTakePictureIntent() {
        val takePictureIntent = Intent(MediaStore.ACTION_IMAGE_CAPTURE)
        if (takePictureIntent.resolveActivity(packageManager) != null) {
            val timeStamp = SimpleDateFormat("yyyyMMdd_HHmmss").format(Date())
            filePath = File(this.filesDir, "$timeStamp.jpg")
            val photoURI = FileProvider.getUriForFile(this, "com.lvtongka.app.fileprovider", filePath!!)
            takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoURI)
            startActivityForResult(takePictureIntent, 20)
        }
    }
}